var do_App = sm("do_App");
var do_Page = sm("do_Page");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var deviceone = require("deviceone");
var couponUrl = require("url/coupon");
var qqHttp = require("helper/http");
var listdata = mm("do_ListData");
var helperOpen = require("helper/open");
var userUtil = require("helper/user");
var noData = [ {
	"template" : 1
} ];
// 本页面-ajax业务请求url
var httpUrl = couponUrl.url.user.managePage;

// UI
var headerAout = ui("headerAout");
var aLayoutRoot = ui("aLayoutRoot");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
var header = ui("header");
var listView = ui("listView");
// 定义变量
var page = 1;
var type;

// andriod返回键
do_Page.on("back", function() {
	do_App.closePage();
});
// 左上角-返回
var headerLeft = ui(addheader + ".headerLeft");
// 右上角-
var headerRight = ui(addheader + ".headerRight");
headerRight.visible = false;
headerLeft.on("touch", function() {
	do_App.closePage()
});
header.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "我的优惠卷"
})
header.refreshData();
// 页面装载完成后，开始初始化工作
do_Page.on("loaded", function() {
	laodPage();
});
listView.bindItems(listdata);
// 刷新数据
// 刷新数据
function laodPage() {
	page = 1;
	var params = {
		page : page
	};
	qqHttp.post(httpUrl, params, function(data) {
		listView.rebound();
		listdata.removeAll();
		if (data.page.content.length>0) {
			for (var i = 0; i < data.page.content.length; i++) {
				data.page.content[i].labelText="点击";
				if(data.page.content[i].bizzType==2){//0.01元购买商品
					data.page.content[i].showText="使用";
				}else{//5元卷元购买商品
					data.page.content[i].showText="查看";
				}
				if(data.page.content[i].status==1){
					data.page.content[i].showBgc="FF7F00FF";
				}else if(data.page.content[i].status==2){//5元卷元购买商品
					data.page.content[i].labelText="已经";
					data.page.content[i].showText="使用";
					data.page.content[i].showBgc="c4c9d0FF";
				}else if(data.page.content[i].status==3){//5元卷元购买商品
					data.page.content[i].labelText="已经";
					data.page.content[i].showText="失效";
					data.page.content[i].showBgc="c4c9d0FF";
				}
			}
			listdata.addData(data.page.content);
		} else {
			listdata.addData(noData);
		}
		listView.refreshItems();
	})
}
// 刷新数据
function getNextPageData() {
	page++;
	var params = {
		page : page
	};
	qqHttp.post(httpUrl, params, function(data) {
		listView.rebound();
		for (var i = 0; i < data.page.content.length; i++) {
			data.page.content[i].labelText="点击";
			if(data.page.content[i].bizzType==2){//0.01元购买商品
				data.page.content[i].showText="使用";
			}else{//5元卷元购买商品
				data.page.content[i].showText="查看";
			}
			if(data.page.content[i].status==1){
				data.page.content[i].showBgc="FF7F00FF";
			}else if(data.page.content[i].status==2){//5元卷元购买商品
				data.page.content[i].labelText="已经";
				data.page.content[i].showText="使用";
				data.page.content[i].showBgc="c4c9d0FF";
			}else if(data.page.content[i].status==3){//5元卷元购买商品
				data.page.content[i].labelText="已经";
				data.page.content[i].showText="失效";
				data.page.content[i].showBgc="c4c9d0FF";
			}
		}
		listdata.addData(data.page.content);
		listView.refreshItems();
	})
}

// 下拉列表，刷新数据
listView.on("pull", function(data) {
	if (data.state == 2) {
		laodPage();
	}
});

// 上拉列表，翻页数据
listView.on("push", function(data) {
	if (data.state == 2) {
		getNextPageData();
	}
});
//点击一条数据
listView.on("touch", function(data1) {
	var ondata = listdata.getOne(data1);
	if(ondata.bizzType==2&&ondata.status==1){//0.01元购买商品
		var params = {
				couponId : ondata.couponId,
				id : ondata.id
			};
			qqHttp.post(couponUrl.url.user.toBuy, params, function(data) {
				if(null!=data.mallShopVo&&data.mallShopVo.type==1){
					toMallOrder(data.mallProduct,ondata.id);
				}else{
					helperOpen.open("source://view/qshop/detail/main.ui",{shopId:data.mallShopVo.id});
	            }
			},function(){
				ondata.strStatus="已使用";
				ondata.showBgc="C0C0C0FF";
				ondata.showBorder="C0C0C0FF,1,[3,4,4,3]";
				listdata.updateOne(data1, ondata);
				listdata.refreshData();
				listView.refreshItems();
			})
	}else if(ondata.bizzType==5&&ondata.status==1){//5元卷元购买商品
		if(ondata.bizzId && ondata.bizzId!=null && ondata.bizzId!=="undefined"){
			helperOpen.open("source://view/qshop/detail/main.ui",{shopId:ondata.bizzId});
		}else{
			nf.alert(ondata.remark);
		}
	}else if(ondata.bizzType==7&&ondata.status==1){//双旦元卷元购买商品
		nf.alert(ondata.remark);
	}else if(ondata.bizzType==3&&ondata.status==1){//服务卷元购买商品
		nf.alert("目前只限日常保洁服务。");
	}else if(ondata.bizzType==8&&ondata.status==1){//服务卷元购买商品
		nf.alert("目前只限周边商家使用。");
	}
});

function toMallOrder(mallProduct,couponUserId){
	if(!userUtil.loginToken()){
		helperOpen.openLogin();	
	}else{
		var total = mallProduct.price;
		var addItems = [];
		var item = {};
		item.productId = mallProduct.productSpecLst[0].id;//默认规格
		item.onlyProductId = mallProduct.id;//id
		item.quantity = 1;
		if(mallProduct.img){
			var imgs = mallProduct.img.split(";");
			item.img = mallProduct.fileDomain + imgs[0];
		}
		item.name = mallProduct.name;
		item.price = mallProduct.productSpecLst[0].price;
		item.marketPrice =mallProduct.productSpecLst[0].marketPrice;
		total = mallProduct.productSpecLst[0].price;
		item.supplierType = mallProduct.supplierTypeCode;
		item.supplierId=mallProduct.supplierId;
		addItems.push(item);
		helperOpen.open("source://view/mall/cart/toOrder.ui",{addItems:addItems,total:total,fromPage:"draw_index","couponUserId":couponUserId},"","pay_product_back");
	}
}
